﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WareHouse.App
{
    using MS.Internal.Xml.XPath;

    using WareHouse.Contract.Models;
    using WareHouse.Contract.Services;
    using WareHouse.Service.Services;

    public partial class FrmPermit : Form
    {
        

        private IWareHouseService wareHouseService;

        public FrmPermit()
        {
            
            wareHouseService = new WareHouseService();
            InitializeComponent();
          
        }

        private void FrmPermit_Load(object sender, EventArgs e)
        {
            LoadGroupUserWithoutAdministrator();
        }

        private void LoadGroupUserWithoutAdministrator()
        {
            glkGroupUser.Properties.DataSource = wareHouseService.ListGroupUserWithoutAdministratorGroup();
            glkGroupUser.Properties.ValueMember = "GroupUserId";
            glkGroupUser.Properties.DisplayMember = "GroupUserName";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView1.DataRowCount > 0)
                {
                    List<DetailGroupUserRoleModel> listModel= new List<DetailGroupUserRoleModel>();
                    if (wareHouseService.DeleteAllRoleByGroupUserId(int.Parse(glkGroupUser.EditValue.ToString())))
                    {

                        for (int i = 0; i < gridView1.DataRowCount; i++)
                        {
                            var newRecord = new DetailGroupUserRoleModel();
                            
                            newRecord.GroupUserId =
                                int.Parse(gridView1.GetRowCellValue(i, "GroupUserId").ToString());
                            newRecord.RoleId = gridView1.GetRowCellValue(i, "RoleId").ToString();
                            newRecord.Permit = bool.Parse(
                                gridView1.GetRowCellValue(i, "Permit").ToString());
                         
                            listModel.Add(newRecord);

                        }

                        if (wareHouseService.SaveListRoleForGroupUser(listModel))
                        {
                              MessageBox.Show("Lưu thành công", "Thông báo");
                            return;
                        }

                          MessageBox.Show("Xãy ra lỗi khi lưu", "Thông báo");
                      


                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Xãy ra lỗi khi lưu", "Thông báo");
            }
        }

        private void glkGroupUser_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                
                gridListRole.DataSource = wareHouseService.GetDetailRoleByGroupUserId(int.Parse(glkGroupUser.EditValue.ToString()));
            }
            catch (Exception)
            {


            }
        }
    }
}
